package pl.edu.pw.perg.compinfodb.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTree;

import org.jdesktop.swingx.JXTable;
import org.springframework.richclient.application.PageComponentContext;
import org.springframework.richclient.application.support.AbstractView;
import org.springframework.richclient.command.ActionCommandExecutor;

import pl.edu.pw.perg.compinfodb.command.AddEventCommandExecutor;
import pl.edu.pw.perg.compinfodb.command.ReloadEventsCommandExecutor;
import pl.edu.pw.perg.compinfodb.command.SaveEventsCommandExecutor;
import pl.edu.pw.perg.compinfodb.datamodel.ConnectorTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.ElementTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.HistoryTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.IEventModificationListener;
import pl.edu.pw.perg.compinfodb.datamodel.SearchTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.TypeTreeModel;
import pl.edu.pw.perg.compinfodb.manager.IElementTypeManager;
import pl.edu.pw.perg.compinfodb.manager.IHistoryManager;

public class ModulesManagerView extends AbstractView {

	private ElementTableModel equipmentTableModel;
	

	public void setTableFactory(CustomRendererTableFactory tableFactory) {
		this.tableFactory = tableFactory;
	}
	private HistoryTableModel historyTableModel;
//	private IElementTypeManager elementTypeManager;
	private TypeTreeModel typeTreeModel;
	private CustomRendererTableFactory tableFactory = new CustomRendererTableFactory();
	private AddEventCommandExecutor addEventExecutor;
	private SaveEventsCommandExecutor saveEventsCommandExecutor;
	private ReloadEventsCommandExecutor reloadEventsCommandExecutor;
	private SearchTableModel searchTableModel = new SearchTableModel();
	
	@Override
	protected JComponent createControl() {

		JPanel view = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.5;
		c.weighty = 0.5;
		c.gridx = 0;
		c.gridy = 0;
		c.gridheight = 1;
		c.gridwidth = 1;
		
		JXTable listTable = tableFactory.getElementTable(equipmentTableModel);
		JXTable historyTable = tableFactory.getTable(historyTableModel);
		JTree typesTree = new JTree(typeTreeModel);
		typesTree.setCellRenderer(new EquipmentTreeCellRenderer(getIconSource()));
		typesTree.setRootVisible(false);
		typesTree.setShowsRootHandles(true);
		typesTree.addTreeSelectionListener(typeTreeModel);
		typeTreeModel.addElementTypeSelectionListener(equipmentTableModel);
		equipmentTableModel.addPhysicalElementSelectionListener(historyTableModel);
		equipmentTableModel.addPhysicalElementSelectionListener(addEventExecutor);
		historyTableModel.addEventModifiactionListener(saveEventsCommandExecutor);
		historyTableModel.addEventModifiactionListener(reloadEventsCommandExecutor);
		JTable search = new JTable(searchTableModel);
		searchTableModel.addTableModelListener(search);
		equipmentTableModel.addColumnModelListener(searchTableModel);
		searchTableModel.addSearchFilterListener(equipmentTableModel);
		equipmentTableModel.recreateColumnModel();
		equipmentTableModel.addTableModelListener(listTable);
		
		JScrollPane typesTreeScrPane = new JScrollPane(typesTree);
		JScrollPane historyScrPane = new JScrollPane(historyTable);
		JScrollPane listScrPane = new JScrollPane(listTable);
		search.setTableHeader(null);
		JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, search, listScrPane);
		jsp.setEnabled(false);
//		JSplitPane connectionSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inputConnectionsScrPane, outputConnectionsScrPane);
//		JSplitPane verticalDownSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, connectionSplitPane, historyScrPane);	
		JSplitPane verticalSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jsp, historyScrPane);
		JSplitPane horizontalSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, typesTreeScrPane, verticalSplitPane); 
		
		verticalSplitPane.setDividerLocation(250);
//		verticalDownSplitPane.setDividerLocation(250);
		horizontalSplitPane.setDividerLocation(250);
		view.add(horizontalSplitPane, c);
		return view;
	}

	public void setEquipmentTableModel(ElementTableModel equipmentTableModel) {
		this.equipmentTableModel = equipmentTableModel;
	}


	public void setHistoryTableModel(HistoryTableModel historyTableModel) {
		this.historyTableModel = historyTableModel;
		addEventExecutor = new AddEventCommandExecutor(historyTableModel);
		reloadEventsCommandExecutor = new ReloadEventsCommandExecutor(historyTableModel);
		saveEventsCommandExecutor = new SaveEventsCommandExecutor(historyTableModel);
	}

	public void setTypeTreeModel(TypeTreeModel typeTreeModel) {
		this.typeTreeModel = typeTreeModel;
	}

	public void setElementTypeManager(IElementTypeManager elementTypeManager) {
//		this.elementTypeManager = elementTypeManager;
		typeTreeModel = new TypeTreeModel(elementTypeManager, false);
	}
	
	@Override
	protected void registerLocalCommandExecutors(PageComponentContext context) {
		context.register("addEventCommand", addEventExecutor);
		context.register("saveEventsCommand", saveEventsCommandExecutor);
		context.register("reloadEventsCommand", reloadEventsCommandExecutor);
	}

}
